package controllers;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import data.Data;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DeleteServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		Utils.checkLoginFalse(request, response);
		
		String sitemap = Utils.fomatSitemap(request.getParameter("link"));
		
		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		if(object!=null)
		{
			DBCollection collection = Data.data("urlCraw");
			
			DBObject dbObject = collection.findOne(new BasicDBObject("sitemap",sitemap).append("username", object.get("username")));
			
			if (dbObject != null) {
				collection.remove(dbObject);
				response.sendRedirect("/add-link");
			}
		} else {
			response.sendRedirect("/login");
		}
	}
}